<template>
  <div>
    <el-row>
      <el-col>
        <el-form :inline="true" :model="formInline">
          <el-form-item>
            <el-input size="mini" v-model="formInline.num" placeholder="请输入设备编号"></el-input>
          </el-form-item>
          <el-form-item>
            <el-input size="mini" v-model="formInline.name" placeholder="请输入设备名称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-select size="mini" clearable v-model="formInline.status" placeholder="请选择状态">
              <el-option label="启用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="search">查询</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <el-row class="buttonList">
      <el-col>
        <el-button type="primary" size="mini" @click="dialogFormVisible = true">预约</el-button>
        <!--<el-button type="primary" size="mini" @click="dialogFormVisible3 = true">延期</el-button>-->
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="4">
        <el-tree accordion highlight-current :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
      </el-col>
      <el-col :span="20">
        <el-table
                :data="tableData"
                size="mini"
                @selection-change="handleSelectionChange"
                border
                style="width: 100%">
          <el-table-column
                  align="center"
                  prop="NUM"
                  label="设备编号">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="NAME"
                  width="100px"
                  label="资产名称">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="CLASSIFICATIONNAME"
                  label="归类属性">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="AMOUNT"
                  label="单价/元">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="SPECS"
                  label="规格">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="MODEL"
                  width="180px"
                  label="型号">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="TYPENUM"
                  label="分类号">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="COMPANYNAME"
                  label="单位名称">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="COMPANY"
                  label="使用单位">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="DIRECTIONNAME"
                  label="使用方向">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="TYPENUM"
                  label="分类号">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="STATUS"
                  label="状态">
            <template slot-scope="scope">
              {{scope.row.STATUS ? '启用': '禁用'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ISVALUABLE"
                  label="是否贵重">
            <template slot-scope="scope">
              {{scope.row.ISVALUABLE ? '是': '否'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ISCONSUMABLES"
                  label="是否易耗">
            <template slot-scope="scope">
              {{scope.row.ISCONSUMABLES ? '是': '否'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ISNORMAL"
                  label="是否正常">
            <template slot-scope="scope">
              {{scope.row.ISNORMAL ? '是': '否'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ISAPPOINTMENT"
                  label="预约">
            <template slot-scope="scope">
              {{scope.row.ISAPPOINTMENT ? '是': '否'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="ISRECEIVE"
                  label="领用">
            <template slot-scope="scope">
              {{scope.row.ISRECEIVE ? '是': '否'}}
            </template>
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="PRESENTNAME"
                  label="现状">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="COUNTRY"
                  label="国别">
          </el-table-column>
          <el-table-column
                  align="center"
                  prop="SOURCENAME"
                  label="来源">
          </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                layout="total, sizes, prev, pager, next, jumper"
                :page-size="pagination.pageSize"
                :total="pagination.total"
                :page-sizes="[10, 20, 30, 50, 100]"
        >
        </el-pagination>
      </el-col>
    </el-row>
    <el-dialog title="设备预约" center :visible.sync="dialogFormVisible" width="50%">
      <el-form :model="form" ref="form" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="12">
            <el-form-item label="使用班级">
              <el-input disabled size="mini" v-model="classesName" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预约人">
              <el-input disabled size="mini" v-model="name" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
                    :rules="{ required: true, message: '请选择日期', trigger: 'change' }"
                    label="预约时间"
                    prop="yuyueTime"
                    class="dateStyle">
              <el-date-picker
                      size="mini"
                      align="right"
                      v-model="form.yuyueTime"
                      unlink-panels
                      value-format="yyyy-MM-dd HH:mm:ss"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      type="datetimerange">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
                    label="归还时间"
                    prop="returnTime"
                    :rules="{ required: true, message: '请选择日期', trigger: 'change' }"
                    class="dateStyle">
              <el-date-picker
                      size="mini"
                      v-model="form.returnTime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      type="datetime"
                      placeholder="选择日期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
            <el-form-item
                    :rules="{ required: true, message: '请选择归还时间', trigger: 'change' }"
                    label="归还时间"
                    prop="returnTime"
                    class="dateStyle">
              <el-date-picker
                      size="mini"
                      v-model="form.returnTime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      type="datetime"
                      placeholder="请选择归还时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
          </el-col>
        </el-row> -->
        <el-row v-for="(list, i) in form.selectList">
          <el-col :span="12">
            <el-form-item
                    label="设备归类"
                    :rules="[{ required: true, message: '请选择归类！', trigger: 'change' }]"
                          :prop="'selectList.' + i + '.type'">
              <el-select clearable size="mini" v-model="list.type" value-key="ID" placeholder="请选择设备归类">
                <el-option
                        v-for="item in selectList"
                        :key="item.ID + i"
                        :label="item.NAME"
                        :value="item">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="数量"
                          :rules="[{ required: true, message: '请选择归类！', trigger: 'change' }]"
                          :prop="'selectList.' + i + '.num'">
              <el-input @input="valNum(list.type.APPOINTMENTNUM, list.num)" :placeholder="'可预约数量' + (list.type.APPOINTMENTNUM || '')" size="mini" v-model="list.num" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="line-height: 40px">
            <el-button size="mini" type="primary" icon="el-icon-remove-outline" plain @click="removeType(i)"></el-button>
          </el-col>
        </el-row>
        <el-row>
          <el-col style="text-align: center">
            <el-button style="width: 28%" size="mini" type="primary" @click="addType">添加</el-button>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="mini" :disabled="btnDisabled" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="设备延期" center :visible.sync="dialogFormVisible3" width="50%">
      <el-form :model="form3" :label-width="formLabelWidth">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备编号">
              <el-input size="mini" v-model="form.region" autocomplete="off" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资产名称">
              <el-input size="mini" v-model="form.name" autocomplete="off" disabled></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="班级">
              <el-input size="mini" v-model="form.banji" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="人数">
              <el-input size="mini" v-model="form.peple" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="预约开始时间" class="dateStyle">
            <el-col :span="11">
              <el-date-picker size="mini" type="date" placeholder="选择日期" v-model="form.date1" style="width: 100%;"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center">-</el-col>
            <el-col :span="11">
              <el-time-picker size="mini" placeholder="选择时间" v-model="form.date2" style="width: 100%;"></el-time-picker>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="预约结束时间" class="dateStyle">
            <el-col :span="11">
              <el-date-picker size="mini" type="date" placeholder="选择日期" v-model="form.date3" style="width: 100%;"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center">-</el-col>
            <el-col :span="11">
              <el-time-picker size="mini" placeholder="选择时间" v-model="form.date4" style="width: 100%;"></el-time-picker>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="延期时间" class="dateStyle">
            <el-col :span="11">
              <el-date-picker size="mini" type="date" placeholder="选择日期" v-model="form.date5" style="width: 100%;"></el-date-picker>
            </el-col>
            <el-col class="line" :span="2" style="text-align: center">-</el-col>
            <el-col :span="11">
              <el-time-picker size="mini" placeholder="选择时间" v-model="form.date6" style="width: 100%;"></el-time-picker>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row v-for="(list, i) in form.selectList">
          <el-col :span="10">
            <el-form-item label="数量">
              <el-input size="mini" v-model="list.num" autocomplete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="设备归类">
              <el-select clearable size="mini" v-model="list.type" placeholder="请选择设备归类">
                <el-option label="测量仪" value="shanghai"></el-option>
                <el-option label="水准仪" value="beijing"></el-option>
                <el-option label="天平" value="beijing2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4" style="line-height: 40px">
            <el-button size="mini" type="primary" icon="el-icon-circle-plus-outline" plain @click="addType"></el-button>
            <el-button size="mini" type="primary" icon="el-icon-remove-outline" plain @click="removeType(i)"></el-button>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogFormVisible3 = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="dialogFormVisible3 = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "equipmentList",
    data() {
      return{
        classesName: '',
        name: '',
        fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}],
        dialogImageUrl: '',
        dialogVisible: false,
        dialogFormVisible3: false,
        form: {
          selectList: [
            {num: '', type: ''}
          ],
          yuyueTime: [],
        },
        form3: {},
        dialogFormVisible: false,
        form2:{
          deviceName: [ {name: ''}, {num: 1212}]
        },
        formLabelWidth: '100px',
        dialogFormVisible2: false,
        formInline: {
          departmentId: '',
          testBaseId: '',
          testId: ''
        },
        pagination: {
          pageSize: 20,
          total: 1,
          pageNo: 1
        },
        data: [],
        defaultProps: {
          children: 'list',
          label: 'NAME'
        },
        tableData: [],
        tableVal: [],
        selectList: [],
        numList: [],
        btnDisabled: false
      }
    },
    created() {
      this.getData(this.pagination.pageSize, this.pagination.pageNo);
      this.classesName = this.$store.getters.getUserMessage.classesName;
      this.name = this.$store.getters.getUserMessage.name;
      this.$post('/insrtumentAppointment/findInstrumentClassificationByAppointment').then(res => {
        this.selectList = res.data.data.dataList;
      });
      this.getTreeData()
    },
    methods: {
      getTreeData() {
        this.$post('/public/findStructureByTestAppointment').then(res => {
          this.data = res.data.data;
        })
      },
      search() {
        this.formInline.pageSize = this.pagination.pageSize;
        this.formInline.pageNo = 1;
        this.$post('/insrtument/findInstrument', this.formInline).then(res => {
          if(res.data.status != 1) {
            this.$message.error('条件输入错误，请检查');
            return;
          }
          this.pagination.total = res.data.data.total;
          this.tableData = res.data.data.dataList;
        })
      },
      getData(pageSize, pageNo) {
        this.$post('/insrtument/findInstrument', {pageSize, pageNo}).then(res => {
          this.tableData = res.data.data.dataList;
          this.pagination.total = res.data.data.total;
        })
      },
      valNum(val, newVal) {
        if(newVal > val) {
          this.$message.error('库存不足，请重新输入');
          this.btnDisabled = true
        } else {
          this.btnDisabled = false
        }
      },
      submit() {
        console.log(this.form);
        let someIte = false
        this.form.selectList.forEach((fIt, fInd) => {
          if (this.form.selectList.some((ite, ind) => fIt.type === ite.type && fInd !== ind)) someIte = true
        })
        if (someIte) return this.$message.error('设备归类不能相同')
        this.$refs['form'].validate((valid) => {
          if (valid) {
            let classification = [];
            for(let i = 0; i < this.form.selectList.length; i++) {
              classification.push(this.form.selectList[i].type.ID + '|' + this.form.selectList[i].num)
            }
            this.$post('/insrtumentAppointment/add',
                    {
                      startTime: this.form.yuyueTime[0],
                      endTime: this.form.yuyueTime[1],
                      returnTime: this.form.returnTime,
                      classification: classification.join()
                    }).then(res => {
                      if(res.data.status === 1) {
                        this.$message.success('预约成功');
                        this.dialogFormVisible = false;
                      } else {
                        this.$message.error(res.data.msg)
                      }
            })
          } else {
            return false;
          }
        })
        //this.dialogFormVisible = false
      },
      handleSizeChange(size) {
        this.pagination.pageSize = size;
        this.getData(size, this.pagination.pageNo);
      },
      handleCurrentChange(page) {
        this.pagination.pageNo = page;
        this.getData(this.pagination.pageSize, page);
      },
      handleSelectionChange(val) {
        this.tableVal = val;
      },
      //添加选项
      addType() {
        this.form.selectList.push({ num: "", type: '' });
      },
      removeType(i) {
        this.form.selectList.splice(i, 1);
      },
      handleNodeClick(data, node) {
        console.log(data);
        this.formInline.departmentId = '';
        this.formInline.testBaseId = '';
        this.formInline.testId = '';
        if(node.level === 1) {
          this.formInline.departmentId = data.ID;
        }
        if(node.level === 2) {
          this.formInline.testBaseId = data.ID;
        }
        if(node.level === 3) {
          this.formInline.testId = data.ID;
        }
        this.search();
      }
    }
  }
</script>

<style scoped lang="scss">
  .search{
    margin-bottom: 10px;
  }
  /deep/.el-select{
    width: 100%;
  }
  .addDev {
    /deep/.el-form-item__content {
      display: flex;
    }
  }
  /deep/.el-divider{
    background: #409eff;
  }
  /deep/.el-divider__text{
    font-size: 18px;
    color: #409eff;
  }
  /deep/.el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%
  }
  /deep/.el-form-item__error {
    top: 32px;
  }
</style>
